package cn.wolfcode.crm.service.imp;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.Menu;
import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.mapper.ClientMapper;
import cn.wolfcode.crm.query.ClientQueryObject;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IClientService;
import cn.wolfcode.crm.util.GetMouth;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ClientServiceImpl implements IClientService {
    @Autowired
    private ClientMapper clientMapper;



    public List<Client> selectAll() {
        return clientMapper.selectAll();
    }

    public Integer selectBirCount() throws ParseException {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(new Date());
        Date date = formatter.parse(dateString);
        return clientMapper.selectBirCount(date);

    }

    public Integer selectbimcount() throws ParseException {
        Map<String,Date> dates = GetMouth.getMouth();
        return clientMapper.selectBimCount(dates);
    }

    public PageResult query(ClientQueryObject qo) {
        Integer total = clientMapper.selectCount(qo);
        if (total == null) {
            return new PageResult(0,Collections.EMPTY_LIST);
        }
        List<Client> rows = clientMapper.queryByQo(qo);
        return new PageResult(total,rows);
    }

    public void importData(MultipartFile file) throws IOException {
        //创建excel文件,将文件导入Excel中
        Workbook wb = new HSSFWorkbook(file.getInputStream());
        //获取工作簿
        Sheet sheet = wb.getSheet("clientData");
        //获取总行数
        int lastRowNum = sheet.getLastRowNum();
        //迭代每一行
        for (int i = 1; i <= lastRowNum ; i++) {
            //把每行的单元格的数据封装到对象中
            Row row = sheet.getRow(i);
            Client client = new Client();
            client.setName(row.getCell(0).getStringCellValue());
            client.setLevel(row.getCell(1).getStringCellValue());
            client.setTel(row.getCell(2).getStringCellValue());
            client.setPassword(row.getCell(3).getStringCellValue());
            clientMapper.insert(client);
        }
    }

    public List<Client> selectExportData(ClientQueryObject qo) {
        return clientMapper.selectForExport(qo);
    }

    public Boolean selectByName(String name, Long id) {
        Client client = clientMapper.selectByName(name);
        return client == null || client.getId()==id;
    }
    public Boolean selectByNumber(String number, Long id) {
        Client client = clientMapper.selectByNumber(number);
        return client == null || client.getId()==id;
    }

    public Integer getCount() {
        return clientMapper.getCount();
    }

    public void loginBysale(String name, String password) {
        Client client = clientMapper.loginBysale(name,password);
        if (client == null) {
            throw new RuntimeException("密码错误");
        }

    }


    public void saveOrUpdate(Client record) {

        if (record.getId() == null) {
            record.setBalance(new BigDecimal("0.00"));
            record.setIntegral(0);
            record.setStatus(true);
            clientMapper.insert(record);

        }else{
            clientMapper.updateByPrimaryKey(record);
        }
    }

    public Client selectByPrimaryKey(Long id) {
        Client client = clientMapper.selectByPrimaryKey(id);
        return client;
    }

    public void deleteByPrimaryKey(Long id) {
        if(id!=null){
            clientMapper.deleteByPrimaryKey(id);
        }
    }
}
